package it.unibo.convoycruisecontrol.boatsubsystem;

import it.unibo.contact.boatSubSystem.SendingNavigationInfoSupport;
import it.unibo.util.information.Information;
import it.unibo.util.information.InformationConverter;

/**
 * @author ale
 * @version 1.0
 * @created 24-ott-2012 00:46:24
 */
public class SendingNavigationInfo extends SendingNavigationInfoSupport 
{
	private INavigationInfo navigationInfo;
	private int DT = 1000;
	private InformationConverter informationConverter;

	
	public SendingNavigationInfo() throws Exception
	{
		super();
	}
	
	/**
	 * 
	 * @param DT 
	 */
	public SendingNavigationInfo(int DT, INavigationInfo navInfo, InformationConverter informationConverter) throws Exception 
	{
		super();
		
		if(navInfo == null || informationConverter == null)
			throw new IllegalArgumentException("The parameters cannot be null");
		if(DT < 0)
			throw new IllegalArgumentException("The sending intervall cannot be negative");
		
		this.DT = DT;
		this.navigationInfo = navInfo;
		this.informationConverter = informationConverter;
	}
	
	public SendingNavigationInfo(INavigationInfo navInfo, InformationConverter messageConverter) throws Exception 
	{
		this(1000, navInfo, messageConverter);
	}
	

	public int getDT()
	{
		return DT;
	}

	@Override
	protected String createInformations() throws Exception 
	{
		Information boatInfo = new Information("boatInfo");
		boatInfo.addAttribute("sequenceNumber", navigationInfo.getSequencePosition()+"");
		boatInfo.addAttribute("speed", navigationInfo.getCurrentSpeed()+"");
		boatInfo.addAttribute("latitude", navigationInfo.getCurrentPosition().getLatitude()+"");
		boatInfo.addAttribute("longitude", navigationInfo.getCurrentPosition().getLongitude()+"");
		return informationConverter.toString(boatInfo);
	}

	@Override
	protected void waitDT() throws Exception 
	{
		Thread.sleep(DT);
	}

}